//
//  LFClockListViewController.m
//  AlarmClock
//
//  Created by LF on 2017/4/26.
//  Copyright © 2017年 LF. All rights reserved.
//

#import "LFClockListViewController.h"
#import "LFAddClockViewController.h"
#import "LFAlartClockTableViewCell.h"

#define kLFAlartClockTableViewCellID @"kLFAlartClockTableViewCellID"

@interface LFClockListViewController ()

@property (nonatomic, strong) LFAlarmClockViewModel *alarmVM; /**< ViewModel */

@end

@implementation LFClockListViewController

- (LFAlarmClockViewModel *)alarmVM
{
    if (!_alarmVM) {
        _alarmVM = [[LFAlarmClockViewModel alloc] init];
    }
    return _alarmVM;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.tableView reloadData];
}

- (void) setupUI {
    
    self.navigationItem.title = @"闹钟列表";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAlarmClock)];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LFAlartClockTableViewCell class]) bundle:nil] forCellReuseIdentifier:kLFAlartClockTableViewCellID];
}

- (void) addAlarmClock {
    LFAddClockViewController *addVc = [[LFAddClockViewController alloc] init];
    [self.navigationController pushViewController:addVc animated:YES];
}


#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.alarmVM.alarmListArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 140;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LFAlartClockTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kLFAlartClockTableViewCellID forIndexPath:indexPath];
    
    cell.dataModel = self.alarmVM.alarmListArray[indexPath.section];
    
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LFAlarmClockModel *model = self.alarmVM.alarmListArray[indexPath.section];
    
    // 创建第一个按钮和触发事件
    UITableViewRowAction *cancelAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除⏰" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
        
        [[LFDBHelper helper] deleteAlarmClock: model];
        
        [self.tableView reloadData];
    }];
    // 定义按钮的颜色
    cancelAction.backgroundColor = [UIColor redColor];
    
    return @[cancelAction];
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LFAddClockViewController *detailViewController = [[LFAddClockViewController alloc] init];
    
    detailViewController.alarmClockModel = self.alarmVM.alarmListArray[indexPath.section];
    
    [self.navigationController pushViewController:detailViewController animated:YES];
}

@end



